package com.winenine999.admin.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.model.Syauthority;
import com.winenine999.model.Syorg;
import com.winenine999.service.ISyauthorityService;
import com.winenine999.service.ISyorgService;

@Controller
@RequestMapping(value="/syorg")
public class SyorgController {
	@Autowired
	private ISyauthorityService syauthorityService;
	@Autowired
	private ISyorgService syorgService;
	/**
	 * 进入权限页面
	 * @return
	 */
	@RequestMapping(value="/syorg",method=RequestMethod.GET)
	public String permission(){
		
		return "message/syorg";
	}
	/**
	 * 给syorg.jsp返回ztree的json数据
	 * @return
	 */
	@RequestMapping(value="/findAllSyorgs",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>>findAllSyorgs(){
		List<Map<String,Object>> maps=syorgService.findAllSyorgs();
		return maps;
	}
	/**
	 * 改权限名
	 * @param syorg
	 * @return
	 */
	@RequestMapping(value="/updateName",method=RequestMethod.POST)
	@ResponseBody
	public Integer updateName(Syorg syorg){
		
		Integer result=	syorgService.updateByPrimaryKeySelective(syorg);
		return result;
	}
	/**
	 * 插入子节点
	 * @param syorg
	 * @return
	 */
	@RequestMapping(value="/insertChildNode",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertChildNode(Syorg syorg){
		Integer result=	syorgService.insertSelective(syorg);
		System.out.println("syorg"+syorg);
		if(syorg.getId()!=null){
			result=syorg.getId();
		}
		return result;
	}
	/**
	 * 移除权限
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	@ResponseBody
	public Integer remove(Integer id){
		
		Integer result=	syorgService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 添加根目录，根目录，添加id(主键自增)，pid(不能和现有id相同，否则会是该节点的子节点),
	 * 为避免添加的pid和已添加的其他节点的id相同，根目录的添加，pid和它的id相等，就避免了这个问题，
	 * 思路：通过添加name,再根据返回的主键id修改pid为id即可
	 * @param syorg
	 * @return
	 */
	@RequestMapping(value="/insertRoot",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertRoot(Syorg syorg){
		
		Integer result=	syorgService.insertSelective(syorg);
		if(syorg.getId()!=null){
			
			syorg.setPid(syorg.getId());
			syorgService.updateByPrimaryKeySelective(syorg);
			result=syorg.getId();
		}
		return result;
	}
}
